package com.sunriz.motordrive.server.common.impl;

import com.sunriz.motordrive.domain.DTO.SetValueDTO;
import com.sunriz.motordrive.domain.DTO.Result;
import com.sunriz.motordrive.server.common.BaseSerial;
import com.sunriz.motordrive.server.common.SerialPortServer;
import com.sunriz.motordrive.server.common.SetValueServer;
import com.sunriz.motordrive.server.photoThermal.Impl.PhotoThermalHcImpl;
import com.sunriz.motordrive.server.photoThermal.Impl.MotorParamServerImpl;
import com.sunriz.motordrive.server.photoVoltaic.Impl.PhotovoltaicHcServerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * ClassName: SerialPortImpl
 * Package: com.sunriz.motordrive.server.common.impl
 * Description:
 *
 * @Author BuTang
 * @Create 2024/7/1 15:29
 * @Version 1.0
 */
@Service
public class SerialPortServerImpl implements SerialPortServer {

    @Resource
    BaseSerial baseSerial;
    @Resource
    SetValueServer setValue;
    @Autowired
    PhotovoltaicHcServerImpl photovoltaicHcServer;


    @Override
    public Result motorEnableSerialPort(String port , int baudRate) {
        BaseSerialImpl.list32 = MotorParamServerImpl.byte32;
        Result result = baseSerial.enableSerialPort(port,baudRate);
        return result;
    }

    @Override
    public Result photoThermalHcEnableSerialPort(String port, int baudRate) {
//        BaseSerialPortImpl.eachAddrByte32 = PhotoThermalHcImpl.eachAddrByte32;
//        SetValueImpl.list32 = PhotoThermalHcImpl.eachAddrByte32;
        BaseSerialImpl.list32 = PhotoThermalHcImpl.byte32;
//        BaseSerialPortImpl.mapOnlyRead = PhotoThermalHcImpl.mapOnlyRead;
        BaseSerialImpl.mapReadAndWrite = PhotoThermalHcImpl.mapReadAndWrite;
        Result result = baseSerial.enableSerialPort(port,baudRate);
        return result;
    }

    @Override
    public Result photoVoltaicHcEnableSerialPort(String port , String slaveId , int baudRate) {

        Integer slaveIdInteger = null;
        try {

            slaveIdInteger = Integer.valueOf(slaveId);
            PhotovoltaicHcServerImpl.slaveId = slaveIdInteger;

            BaseSerialImpl.list32 = PhotovoltaicHcServerImpl.byte32;
            BaseSerialImpl.mapReadAndWrite = PhotovoltaicHcServerImpl.table2;
            Result result = baseSerial.enableSerialPort(port,baudRate);
            return result;
        } catch (NumberFormatException e) {
            return Result.fail("请输入正确的slaveId","403");
        }


    }

    @Override
    public Result disableSerialPort() {
        BaseSerialImpl.returnRead.clear();
        BaseSerialImpl.returnReadAndWrite.clear();
        Result result = baseSerial.disableSerialPort();
        BaseSerialImpl.messageSendingFailure = 0;
        return result;
    }

    @Override
    public Result connectStatus() {
        if (BaseSerialImpl.messageSendingFailure > BaseSerialImpl.closeTheSerialPortNum){
            disableSerialPort();
            return Result.fail("串口物理连接可能断开","403");
        }else {
            return Result.success("串口物理连接正常","200");
        }
    }

    @Override
    public Result setValue(SetValueDTO setValueDTO) {
        Result result = setValue.single(setValueDTO);
        return result;
    }

    @Override
    public Result check(SetValueDTO setValueDTO) {
        Result result = setValue.check(setValueDTO);
        return result;
    }

    @Override
    public Result getPorts() {
        Result result = baseSerial.getPorts();
        return result;
    }

    @Override
    public Result clearData() {
        BaseSerialImpl.returnRead.clear();
        BaseSerialImpl.returnReadAndWrite.clear();
        return null;
    }
}
